<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单类型" prop="orderType">
        <el-select v-model="queryParams.orderType" placeholder="请选择订单类型" clearable>
          <el-option
            v-for="dict in dict.type.order_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="orderStatus">
        <el-select v-model="queryParams.orderStatus" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="orderCreateTime">
        <el-date-picker clearable
                        v-model="queryParams.orderCreateTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择下单时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入库状态" prop="storeStatus">
        <el-select v-model="queryParams.storeStatus" placeholder="请选择入库状态" clearable>
          <el-option
            v-for="dict in dict.type.store_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入库时间" prop="storeTime">
        <el-date-picker clearable
                        v-model="queryParams.storeTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择入库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="打包状态" prop="packStatus">
        <el-select v-model="queryParams.packStatus" placeholder="请选择打包状态" clearable>
          <el-option
            v-for="dict in dict.type.pack_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="打包时间" prop="packTime">
        <el-date-picker clearable
                        v-model="queryParams.packTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择打包时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="出库状态" prop="outboundStatus">
        <el-select v-model="queryParams.outboundStatus" placeholder="请选择出库状态" clearable>
          <el-option
            v-for="dict in dict.type.outbound_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出库时间" prop="outboundTime">
        <el-date-picker clearable
                        v-model="queryParams.outboundTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择出库时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="盘点状态" prop="stocktakingStatus">
        <el-select v-model="queryParams.stocktakingStatus" placeholder="请选择盘点状态" clearable>
          <el-option
            v-for="dict in dict.type.stocktaking_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="盘点时间" prop="stocktakingTime">
        <el-date-picker clearable
                        v-model="queryParams.stocktakingTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择盘点时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请寄件" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择申请寄件" clearable>
          <el-option
            v-for="dict in dict.type.send_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="寄件时间" prop="sendTime">
        <el-date-picker clearable
                        v-model="queryParams.sendTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择寄件时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <div v-loading="loading" class="card-container">
      <el-row :gutter="20">
        <el-col
          v-for="order in infoList"
          :key="order.id"
          :xs="24" :sm="12" :md="12" :lg="8"
        >
          <el-card class="order-card">
            <!-- 卡片头部 -->
            <div slot="header" class="card-header">
              <div class="header-left">
                <el-checkbox
                  v-model="selectedOrders"
                  :label="order.id"
                  v-if="multiple"
                ></el-checkbox>
                <span class="order-number">{{ order.orderNumber }}</span>
              </div>
              <div class="header-right">
                <dict-tag
                  :options="dict.type.order_status"
                  :value="order.orderStatus"
                  size="small"
                />
              </div>
            </div>

            <!-- 订单基本信息 -->
            <div class="order-base-info">
              <div class="info-item">
                <label>用户：</label>
                <span>{{ order.userName }}</span>
              </div>
              <div class="info-item">
                <label>下单时间：</label>
                <span>{{ parseTime(order.orderCreateTime) }}</span>
              </div>
            </div>

            <!-- 商品表格 -->
            <el-table
              :data="order.goodsList"
              size="mini"
              class="goods-table"
              stripe
              border
            >
              <el-table-column label="商品ID" prop="id" width="80" align="center"/>
              <el-table-column label="商品名称" prop="name" min-width="120"/>
              <el-table-column label="单价" width="100" align="right">
                <template slot-scope="{row}">
                  ￥{{ row.price.toFixed(2) }}
                </template>
              </el-table-column>
              <el-table-column label="数量" prop="quantity" width="80" align="center"/>
              <el-table-column label="小计" width="120" align="right">
                <template slot-scope="{row}">
                  ￥{{ (row.price * row.quantity).toFixed(2) }}
                </template>
              </el-table-column>
            </el-table>

            <!-- 订单状态时间线 -->
            <div class="status-timeline">
              <div v-for="status in statusList" :key="status.prop" class="status-item">
                <div class="status-label">{{ status.label }}：</div>
                <div class="status-content">
                  <dict-tag
                    :options="dict.type[status.dictType]"
                    :value="order[status.prop]"
                    size="mini"
                  />
                  <div v-if="order[status.timeProp]" class="status-time">
                    {{ parseTime(order[status.timeProp], '{y}-{m}-{d}') }}
                  </div>
                </div>
              </div>
            </div>

            <!-- 操作按钮 -->
            <div class="card-actions">
              <el-button
                type="primary"
                size="mini"
                @click="handleUpdate(order)"
                v-hasPermi="['system:info:edit']"
              >修改</el-button>
              <el-button
                type="danger"
                size="mini"
                @click="handleDelete(order)"
                v-hasPermi="['system:info:remove']"
              >删除</el-button>
            </div>
          </el-card>
        </el-col>
      </el-row>

      <!-- 分页组件保持原样 -->
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>



    <!-- 添加或修改订单操作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <div class="dynamic-form">
        <!-- 表单切换内容 -->
        <transition name="el-fade-in" mode="out-in">
          <!-- 基本信息模块 -->
          <el-form
            v-if="activeTab === 'basic'"
            key="basic"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="basicForm"
          >
            <el-form-item label="订单编号" prop="orderNumber">
              <el-input v-model="form.orderNumber" placeholder="请输入订单编号" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="订单类型" prop="orderType">
              <el-select v-model="form.orderType" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.order_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="用户名称" prop="userName">
              <el-input v-model="form.userName" placeholder="请输入用户名称" style="width: 217px"/>
            </el-form-item>
            <el-form-item label="下单时间" prop="orderCreateTime">
              <el-date-picker
                v-model="form.orderCreateTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择下单时间"
                style="width: 217px"
              />
            </el-form-item>
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-form>

          <!-- 状态信息模块 -->
          <el-form
            v-else
            key="status"
            :model="form"
            label-width="100px"
            class="form-section"
            :rules="rules"
            ref="statusForm"
          >
            <el-form-item label="订单状态">
              <el-radio-group v-model="form.orderStatus">
                <el-radio
                  v-for="dict in dict.type.order_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"

                >{{ dict.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库状态" style="margin-top: 8px">
              <el-radio-group v-model="form.storeStatus">
                <el-radio
                  v-for="dict in dict.type.store_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="入库时间">
              <el-date-picker
                v-model="form.storeTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择入库时间"
              />
            </el-form-item>
            <el-form-item label="打包状态">
              <el-radio-group v-model="form.packStatus">
                <el-radio
                  v-for="dict in dict.type.pack_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="打包时间">
              <el-date-picker
                v-model="form.packTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择打包时间"
              />
            </el-form-item>

            <el-form-item label="出库状态">
              <el-radio-group v-model="form.outboundStatus">
                <el-radio
                  v-for="dict in dict.type.outbound_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="出库时间">
              <el-date-picker
                v-model="form.outboundTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择出库时间"
              />
            </el-form-item>

            <el-form-item label="盘点状态">
              <el-radio-group v-model="form.stocktakingStatus">
                <el-radio
                  v-for="dict in dict.type.stocktaking_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="盘点时间">
              <el-date-picker
                v-model="form.stocktakingTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择盘点时间"
              />
            </el-form-item>

            <el-form-item label="申请寄件">
              <el-radio-group v-model="form.sendStatus">
                <el-radio
                  v-for="dict in dict.type.send_status"
                  :key="dict.value"
                  :label="parseInt(dict.value)"
                >
                  {{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="寄件时间">
              <el-date-picker
                v-model="form.sendTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择寄件时间"
              />
            </el-form-item>
          </el-form>
        </transition>

        <!-- 切换按钮组 -->
        <div class="button-group">
          <el-button
            :type="activeTab === 'basic' ? 'primary' : 'info'"
            @click="activeTab = 'basic'"
          >订单基本信息</el-button>
          <el-button
            :type="activeTab === 'status' ? 'primary' : 'info'"
            @click="activeTab = 'status'"
          >订单状态信息</el-button>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/system/info";

export default {
  name: "Info",
  dicts: ['order_status', 'pack_status', 'send_status', 'order_type', 'outbound_status', 'store_status', 'stocktaking_status'],
  data() {
    return {





      selectedOrders: [],  // 选中的订单ID数组
      statusList: [  // 状态展示配置
        { label: '入库状态', prop: 'storeStatus', dictType: 'store_status', timeProp: 'storeTime' },
        { label: '打包状态', prop: 'packStatus', dictType: 'pack_status', timeProp: 'packTime' },
        { label: '出库状态', prop: 'outboundStatus', dictType: 'outbound_status', timeProp: 'outboundTime' },
        { label: '寄件状态', prop: 'sendStatus', dictType: 'send_status', timeProp: 'sendTime' }
      ],






      activeTab: 'basic', // 当前激活的标签页
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单操作表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNumber: null,
        orderType: null,
        orderStatus: null,
        orderCreateTime: null,
        storeStatus: null,
        storeTime: null,
        packStatus: null,
        packTime: null,
        outboundStatus: null,
        outboundTime: null,
        stocktakingStatus: null,
        stocktakingTime: null,
        sendStatus: null,
        sendTime: null,
        userName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderNumber: [
          { required: true, message: "订单编号不能为空", trigger: "blur" }
        ],
        orderType: [
          { required: true, message: "订单类型不能为空", trigger: "change" }
        ],
        orderStatus: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        orderCreateTime: [
          { required: true, message: "下单时间不能为空", trigger: "blur" }
        ],
        storeStatus: [
          { required: true, message: "入库状态不能为空", trigger: "blur" }
        ],
        packStatus: [
          { required: true, message: "打包状态不能为空", trigger: "blur" }
        ],
        outboundStatus: [
          { required: true, message: "出库状态不能为空", trigger: "blur" }
        ],
        stocktakingStatus: [
          { required: true, message: "盘点状态不能为空", trigger: "blur" }
        ],
        sendStatus: [
          { required: true, message: "申请寄件不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "用户名称不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单操作列表 */
    getList() {
      this.loading = true;
      listInfo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderNumber: null,
        orderType: null,
        orderStatus: 1,
        orderCreateTime: null,
        storeStatus: 2,
        storeTime: null,
        packStatus: 1,
        packTime: null,
        outboundStatus: 1,
        outboundTime: null,
        stocktakingStatus: 1,
        stocktakingTime: null,
        sendStatus: 1,
        sendTime: null,
        userName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单操作";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单操作";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 根据当前标签获取对应的表单引用
      const formRef = this.activeTab === 'basic' ? 'basicForm' : 'statusForm';
      this.$refs[formRef].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单操作编号为"' + ids + '"的数据项？').then(function() {
        return delInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/info/export', {
        ...this.queryParams
      }, `info_${new Date().getTime()}.xlsx`)
    },
    toggleTab() {
      this.activeTab = this.activeTab === 'basic' ? 'status' : 'basic'
    }
  }
};
</script>

<style scoped>
/* 卡片容器 */
.card-container {
  padding: 15px;
  /*width: 4000px;*/
}

.order-card {
  margin-bottom: 20px;
  transition: all 0.3s;
}

.order-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

/* 卡片头部 */
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}
.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}
.order-number {
  font-weight: 500;
  color: #303133;
}

/* 基础信息 */
.order-base-info {
  margin: 12px 0;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  font-size: 13px;
}

.info-item {
  display: flex;
  gap: 6px;
}
label {
  color: #909399;
  white-space: nowrap;
}

span {
  color: #606266;
}


/* 商品表格 */
.goods-table {
  margin: 10px 0;
}
::v-deep .el-table__header th {
  background: #f5f7fa;
}

::v-deep .el-table__body {
  font-size: 12px;
}


/* 状态时间线 */
.status-timeline {
  margin-top: 15px;
  padding: 10px;
  background: #f8f9fa;
  border-radius: 4px;
}
.status-item {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
}
.status-label {
  width: 80px;
  color: #909399;
  font-size: 12px;
}

.status-content {
  flex: 1;
}
.status-time {
  color: #c0c4cc;
  font-size: 12px;
  margin-top: 2px;
}

/* 操作按钮 */
.card-actions {
  margin-top: 15px;
  text-align: right;
}
.el-button {
  padding: 6px 8px;
  margin-left: 10px;
}


/* 响应式适配 */
@media (max-width: 768px) {
  .order-base-info {
    grid-template-columns: 1fr;
  }

  .card-actions {
    text-align: center;
  }
  .el-button {
    width: 48%;
    margin: 0 1%;
  }
}

.goods-table {
  overflow-x: auto;
}
::v-deep .el-table {
  width: 120%;
}

</style>
